package pCloud.client;

import java.net.InetSocketAddress;
import java.util.Random;

import org.apache.log4j.FileAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;

import pCloud.config.PCloudConfig;
import pCloud.config.PCloudDirectRequestConfig;
import pCloud.message.cloud.DataMessage;
import pCloud.message.cloud.HandShakeMessage;
import pCloud.transport.Communicator;
import pCloud.transport.SocketCommunicator;
import pCloud.transport.TransportHandle;

public class DataOwnerSW {
	Connector connector;
	InetSocketAddress cloudAddress;

	Communicator communicator;
	int clientId, beta;
	Logger logger;

	String configFile = PCloudConfig.DEFAULT_CONFIG_FILE;
	InetSocketAddress localAddress;
	
	public DataOwnerSW(String configFile, int id) throws Exception {		
		this.communicator = new SocketCommunicator();
		this.configFile = configFile;
		this.clientId = id;
		init();
		startConnection();
	}

	void init() throws Exception {
		PCloudConfig.init(this.configFile);
				
		logger = initLog("Owner" + clientId);
		communicator.init(logger);

		this.beta = new Integer(PCloudConfig.getString("beta")).intValue();
		
		String[] server = PCloudConfig.getString(
				PCloudDirectRequestConfig.SERVER).split(" ");
		InetSocketAddress serverAdd = new InetSocketAddress(server[0],
				new Integer(server[1]).intValue());

		connector = new DataOwnerCloudConnector(serverAdd.getHostName(),
				serverAdd.getPort(), this.communicator);

		cloudAddress = serverAdd;		
	}

	Logger initLog(String name) {
		Logger logger = Logger.getLogger(name);
		SimpleLayout layout = new SimpleLayout();

		FileAppender appender = null;
		try {
			appender = new FileAppender(layout,
					PCloudConfig.getString(PCloudConfig.PCLOUD_CLIENT_LOG),
					false);
		} catch (Exception e) {
			System.err.println("Error initializing log file for " + name);
			e.printStackTrace();
		}

		logger.addAppender(appender);
		return logger;
	}

	private void startConnection() throws Exception {
		connector.connect();
		this.communicator.addConnection(connector.protocolHandler);
		this.localAddress = (InetSocketAddress) connector.protocolHandler
				.getTransportHanlde().getLocalAddress();
		System.out.println("Connected to the cloud");
	}
	
	//send a HandShakeMessage, then DataMessage values
	public void doIt() throws Exception{		
				
		//1. HandShakeMessage
		HandShakeMessage hsm = new HandShakeMessage(true,this.clientId); 
		TransportHandle conn = this.communicator.getConnection(this.cloudAddress);
		conn.send(hsm);
		
		//2. Intializate cryto
		DataOwnerCryptoHandle doch = new DataOwnerCryptoHandle(conn,
				this.clientId, this.beta, true);
		doch.initialize();
				
		//3. then sending DataMessage, every 1s
		int val = 0;
		int idx = 0;
		int sendRate = new Integer(PCloudConfig.getString("send_rate"))
				.intValue();	
		DataEncryptionHelper deh = new DataEncryptionHelper(doch);
		deh.start();
		while (true){
			Thread.sleep(sendRate);						
			//doch.encryptAndSendToTheCloud(idx, val);
			deh.setVals(idx, val);
			val++;			
			idx++;
		}
	}
	
	public static void main(String[] args) throws Exception{
		DataOwnerSW dat = new DataOwnerSW(args[0],new Integer(args[1]).intValue());
		dat.doIt();
	}
}
